﻿using System;
using FastNet.RepositorySystem;
using FastNet.RepositorySystem.Interfaces;
using SampleRepository.Entities;

namespace SampleStore.Entities
{
    /// <summary>
    /// Entity for Contact Data
    /// </summary>
    internal partial class Contact : IContactData
    {
        /// <summary>
        /// Initializes a new instance of the Contact class
        /// </summary>
        public Contact()
        {
            ChangeTracker.ChangeTrackingEnabled = true;
            ChangeTracker.ObjectStateChanging += HandleDateChanging;
            DateCreated = DateTime.UtcNow;
            DateModified = DateTime.UtcNow;
        }

        /// <summary>
        /// Handles changing of the dates when things happen
        /// </summary>
        /// <param name="sender">ChangeTracker calling this method</param>
        /// <param name="e">ObjectStateChangingEventArgs with information about the event</param>
        private void HandleDateChanging(object sender, ObjectStateChangingEventArgs e)
        {
            if (e.NewState == ObjectState.Modified)
            {
                DateModified = DateTime.UtcNow;
            }
        }

        /// <summary>
        /// Marks the entity for deletion but does not actually remove it from the database
        /// </summary>
        internal void MarkedForDeletion()
        {
            DateDeleted = DateTime.UtcNow;
        }

        #region Implementation of IData

        /// <summary>
        /// Gets a value indicating whether or not this instance has been changed
        /// </summary>
        bool IData.IsDirty { get { return ChangeTracker.State == ObjectState.Modified; } }

        /// <summary>
        /// Gets a value indicating whether or not this instance is a new entity
        /// </summary>
        bool IData.IsNew { get { return ChangeTracker.State == ObjectState.Added; } }

        /// <summary>
        /// Gets a value indicating whether or not this instance has been deleted
        /// </summary>
        bool IData.IsDeleted { get { return DateDeleted.HasValue || ChangeTracker.State == ObjectState.Deleted; } }

        #endregion

        #region Identities

        /// <summary>
        /// Gets the Identifier for the instance
        /// </summary>
        Identity IIdentityData.Id { get { return Id; } }

        #endregion

        #region Implementation of IContactData

        Identity IContactData.ShippingAddressId
        {
            get { return ShippingAddressId == Guid.Empty ? Identity.Empty : (Identity)ShippingAddressId; }
            set { ShippingAddressId = (value == Identity.Empty ? Guid.Empty : (Guid)value); }
        }

        Identity IContactData.BillingAddressId
        {
            get { return BillingAddressId == Guid.Empty ? Identity.Empty : (Identity)BillingAddressId; }
            set { BillingAddressId = (value == Identity.Empty ? Guid.Empty : (Guid)value); }
        }

        #endregion
    }
}
